#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n;
cin>>n;
vector<int> v(n);
map<int,int>m;
for(int i=0;i<n;i++){
cin>>v[i];
m[v[i]]++;
}
if(m.size()!=n){
cout<<-1;
return ;
}
vector<int> adj[n+1];
vector<int> vis(n+1,0);
for(int i=0;i<n;i++){
adj[i+1].push_back(v[i]);
}
vector<int> ans;
for(int i=0;i<n;i++){
vis[i+1]=1;
queue<int> q;
int cnt=0;
q.push(i+1);
while(!q.empty()){
int node=q.front();
q.pop();
cnt++;
for(auto it: adj[node]){
if(vis[it]==0){
vis[it]=1;
q.push(it);
}
}
}
for(int i=0;i<n;i++){
vis[i+1]=0;
}
if(cnt%2==0){
ans.push_back(cnt/2);
}
else{
ans.push_back(cnt);
}
}
int lcm=ans[0];
for(int i=1;i<ans.size();i++){
int gcdd=__gcd(lcm,ans[i]);
lcm=(lcm*ans[i])/gcdd;
}
cout<<lcm<<endl;
}
int32_t main()
{
int t = 1;
// cin>>t;
while (t--)
{
solve();
}
return 0;
}
1490E - Accidental Victory | 1335A - Candies and Two Sisters |
96B - Lucky Numbers (easy) | 1151B - Dima and a Bad XOR |
1435B - A New Technique | 1633A - Div 7 |
268A - Games | 1062B - Math |
1294C - Product of Three Numbers | 749A - Bachgold Problem |
1486B - Eastern Exhibition | 1363A - Odd Selection |
131B - Opposites Attract | 490C - Hacking Cypher |
158B - Taxi | 41C - Email address |
1373D - Maximum Sum on Even Positions | 1574C - Slay the Dragon |
621A - Wet Shark and Odd and Even | 1395A - Boboniu Likes to Color Balls |
1637C - Andrew and Stones | 1334B - Middle Class |
260C - Balls and Boxes | 1554A - Cherry |
11B - Jumping Jack | 716A - Crazy Computer |
644A - Parliament of Berland | 1657C - Bracket Sequence Deletion |
1657B - XY Sequence | 1009A - Game Shopping |